*up the # of max variables beyond 5000
set maxvar 8000 

*Load data, set replication governance folder as your base file folder

cd "C:\Users\sam2334\Dropbox\vdem-aid"

*load raw vdem data, make cow_code name identical, eliminate vanishingly rare missing cow
*for practical resons, this vdem dataset has been edited from the original (1+ gigabyte) file to only have the varibles from vdem8 that the paper analysis uses

use "ReplicationGovernance\Finkelreplication\Vdem8\V-Dem-CY+Others-v8.dta", clear

gen cc_cow=COWcode
drop if cc_cow==.
drop if cc_cow==99999

xtset COWcode year

sort COWcode year

***********************Repeat observations of electoral variables
*need to generate off-election estimates of variables


replace v2elrgstry = L.v2elrgstry if v2elrgstry >= .
replace v2elvotbuy = L.v2elvotbuy if v2elvotbuy >= .
replace v2elirreg = L.v2elirreg if v2elirreg >= .
replace v2elintim = L.v2elintim if v2elintim >= .
replace v2elpeace = L.v2elpeace if v2elpeace >= .
replace v2elmulpar = L.v2elmulpar if v2elmulpar >= .
replace v2elboycot = L.v2elboycot if v2elboycot >= .

*save for future merge

save "ReplicationGovernance\Finkelreplication\Vdem8\V-Dem-CY+Others-v8cowchange.dta", replace

clear

*Load Finkel et al 2007 data, converted to .dta, from their replication package.

use "ReplicationGovernance\Finkelreplication\Finkel\USAID_DG_Programs.dta", clear

*xtmixed setup

egen countrycode = group(cname)

xtset countrycode year

*merge in previous vdem data to finkel data

merge m:1 cc_cow year using "ReplicationGovernance\Finkelreplication\Vdem8\V-Dem-CY+Others-v8cowchange.dta", generate(_mergevdem)

*drop thousands and thousands of observations in vdem but not in short finkel data

drop if _merge==2

******Save file here for synthetic control pre-panel analysis

save "ReplicationGovernance\Finkelreplication\Vdem8\finkelmerge.dta", replace

*************************test micro component suffrage/participation******************************

xtset countrycode year

*MODEL 4 aid on voter registry accuracy

xtmixed v2elrgstry year AID100 AID000 AID_2 SAID100 SAID000 ODA100 ODA000 PRF01I DIF07 FPP01 POL05 POL25 L203 L2999a L220 L223 L221 L225 L222 L212 c.year#c.L203 c.year#c.L2999a c.year#c.L220 c.year#c.L223 c.year#c.L221 c.year#c.L225 c.year#c.L222 c.year#c.L212 || countrycode: year, vce(robust) residuals(ar1, t(year))

*MODEL 5 aid on suffrage de facto (% older than voting age)

xtmixed v2asuffrage year AID100 AID000 AID_2 SAID100 SAID000 ODA100 ODA000 PRF01 DIF07 FPP01 POL05 POL25 L203 L2999a L220 L223 L221 L225 L222 L212 c.year#c.L203 c.year#c.L2999a c.year#c.L220 c.year#c.L223 c.year#c.L221 c.year#c.L225 c.year#c.L222 c.year#c.L212 || countrycode: year, vce(robust) residuals(ar1, t(year))

*************************test micro component election management (EMB)******************************

*MODEL 6 aid on EMB autonomy

xtmixed v2elembaut year AID100 AID000 AID_2 SAID100 SAID000 ODA100 ODA000 PRF01 DIF07 FPP01 POL05 POL25 L203 L2999a L220 L223 L221 L225 L222 L212 c.year#c.L203 c.year#c.L2999a c.year#c.L220 c.year#c.L223 c.year#c.L221 c.year#c.L225 c.year#c.L222 c.year#c.L212 || countrycode: year, vce(robust) residuals(ar1, t(year))

*MODEL 7 aid on EMB capacity

xtmixed v2elembcap year AID100 AID000 AID_2 SAID100 SAID000 ODA100 ODA000 PRF01 DIF07 FPP01 POL05 POL25 L203 L2999a L220 L223 L221 L225 L222 L212 c.year#c.L203 c.year#c.L2999a c.year#c.L220 c.year#c.L223 c.year#c.L221 c.year#c.L225 c.year#c.L222 c.year#c.L212 || countrycode: year, vce(robust) residuals(ar1, t(year))


*************************test micro outcomes---election irregularities/quality******************************

*MODEL 8 aid on Vote Buying

xtmixed v2elvotbuy year AID100 AID000 AID_2 SAID100 SAID000 ODA100 ODA000 PRF01 DIF07 FPP01 POL05 POL25 L203 L2999a L220 L223 L221 L225 L222 L212 c.year#c.L203 c.year#c.L2999a c.year#c.L220 c.year#c.L223 c.year#c.L221 c.year#c.L225 c.year#c.L222 c.year#c.L212 || countrycode: year, vce(robust) residuals(ar1, t(year))

*MODEL 9 aid on other voting irregularities

xtmixed v2elirreg year AID100 AID000 AID_2 SAID100 SAID000 ODA100 ODA000 PRF01 DIF07 FPP01 POL05 POL25 L203 L2999a L220 L223 L221 L225 L222 L212 c.year#c.L203 c.year#c.L2999a c.year#c.L220 c.year#c.L223 c.year#c.L221 c.year#c.L225 c.year#c.L222 c.year#c.L212 || countrycode: year, vce(robust) residuals(ar1, t(year))

*MODEL 10 aid on government intimidation

xtmixed v2elintim year AID100 AID000 AID_2 SAID100 SAID000 ODA100 ODA000 PRF01 DIF07 FPP01 POL05 POL25 L203 L2999a L220 L223 L221 L225 L222 L212 c.year#c.L203 c.year#c.L2999a c.year#c.L220 c.year#c.L223 c.year#c.L221 c.year#c.L225 c.year#c.L222 c.year#c.L212 || countrycode: year, vce(robust) residuals(ar1, t(year))

*MODEL 11 aid on other electoral violence

xtmixed v2elpeace year AID100 AID000 AID_2 SAID100 SAID000 ODA100 ODA000 PRF01 DIF07 FPP01 POL05 POL25 L203 L2999a L220 L223 L221 L225 L222 L212 c.year#c.L203 c.year#c.L2999a c.year#c.L220 c.year#c.L223 c.year#c.L221 c.year#c.L225 c.year#c.L222 c.year#c.L212 || countrycode: year, vce(robust) residuals(ar1, t(year))


***************************test micro outcomes---Party competition************************

*MODEL 12 Was this election multiparty

xtmixed v2elmulpar year AID100 AID000 AID_2 SAID100 SAID000 ODA100 ODA000 PRF01 DIF07 FPP01 POL05 POL25 L203 L2999a L220 L223 L221 L225 L222 L212 c.year#c.L203 c.year#c.L2999a c.year#c.L220 c.year#c.L223 c.year#c.L221 c.year#c.L225 c.year#c.L222 c.year#c.L212 || countrycode: year, vce(robust) residuals(ar1, t(year))

***************************micro *aid* to micro outcome

********elections aid to election management

*MODEL 1 election aid on voter registry accuracy

xtmixed v2elrgstry year AID110 AID000 AID_2 SAID100 SAID000 ODA100 ODA000 PRF01 DIF07 FPP01 POL05 POL25 L203 L2999a L220 L223 L221 L225 L222 L212 c.year#c.L203 c.year#c.L2999a c.year#c.L220 c.year#c.L223 c.year#c.L221 c.year#c.L225 c.year#c.L222 c.year#c.L212 || countrycode: year, vce(robust) residuals(ar1, t(year))

*MODEL 2 election aid on EMB autonomy

xtmixed v2elembaut year AID110 AID000 AID_2 SAID100 SAID000 ODA100 ODA000 PRF01 DIF07 FPP01 POL05 POL25 L203 L2999a L220 L223 L221 L225 L222 L212 c.year#c.L203 c.year#c.L2999a c.year#c.L220 c.year#c.L223 c.year#c.L221 c.year#c.L225 c.year#c.L222 c.year#c.L212 || countrycode: year, vce(robust) residuals(ar1, t(year))

*MODEL 3 election aid on EMB capacity

xtmixed v2elembcap year AID110 AID000 AID_2 SAID100 SAID000 ODA100 ODA000 PRF01 DIF07 FPP01 POL05 POL25 L203 L2999a L220 L223 L221 L225 L222 L212 c.year#c.L203 c.year#c.L2999a c.year#c.L220 c.year#c.L223 c.year#c.L221 c.year#c.L225 c.year#c.L222 c.year#c.L212 || countrycode: year, vce(robust) residuals(ar1, t(year))


**************************Aggregate Outcomes*****************************

*MODEL 14 polyarchy/institutional

xtmixed v2x_polyarchy year AID100 AID000 AID_2 SAID100 SAID000 ODA100 ODA000 PRF01 DIF07 FPP01 POL05 POL25 L203 L2999a L220 L223 L221 L225 L222 L212 c.year#c.L203 c.year#c.L2999a c.year#c.L220 c.year#c.L223 c.year#c.L221 c.year#c.L225 c.year#c.L222 c.year#c.L212 || countrycode: year, vce(robust) residuals(ar1, t(year))

*MODEL 15 Liberal democracy index

xtmixed v2x_libdem year AID100 AID000 AID_2 SAID100 SAID000 ODA100 ODA000 PRF01 DIF07 FPP01 POL05 POL25 L203 L2999a L220 L223 L221 L225 L222 L212 c.year#c.L203 c.year#c.L2999a c.year#c.L220 c.year#c.L223 c.year#c.L221 c.year#c.L225 c.year#c.L222 c.year#c.L212 || countrycode: year, vce(robust) residuals(ar1, t(year))

*MODEL 13 aid on clean elections

xtmixed v2xel_frefair year AID100 AID000 AID_2 SAID100 SAID000 ODA100 ODA000 PRF01 DIF07 FPP01 POL05 POL25 L203 L2999a L220 L223 L221 L225 L222 L212 c.year#c.L203 c.year#c.L2999a c.year#c.L220 c.year#c.L223 c.year#c.L221 c.year#c.L225 c.year#c.L222 c.year#c.L212 || countrycode: year, vce(robust) residuals(ar1, t(year))

*Summary statistics and descriptives if all independent variables are present (so in test)
*IVs
sum AID110 AID100 AID000 AID_2 SAID100 SAID000 ODA100 ODA000 PRF01 DIF07 FPP01 POL05 POL25 L203 L2999a L220 L223 L221 L225 L222 L212 if AID100!=. & AID100!=. & AID000!=. & AID_2!=. & SAID100!=. & SAID000!=. & ODA100!=. & ODA000!=. & PRF01!=. & DIF07!=. & FPP01!=. & POL05!=. & POL25!=. & L203!=. & L2999a!=. & L220!=. & L223!=. & L221!=. & L225!=. & L222!=. & L212!=.
*DVs
sum v2elrgstry v2asuffrage v2elembaut v2elembcap v2elvotbuy v2elirreg v2elintim v2elpeace v2elmulpar v2elboycot v2xel_frefair v2x_polyarchy v2x_libdem if AID100!=. & AID100!=. & AID000!=. & AID_2!=. & SAID100!=. & SAID000!=. & ODA100!=. & ODA000!=. & PRF01!=. & DIF07!=. & FPP01!=. & POL05!=. & POL25!=. & L203!=. & L2999a!=. & L220!=. & L223!=. & L221!=. & L225!=. & L222!=. & L212!=.

*MODEL 16 Backwards regressions predicting aid

xtmixed AID100 year v2x_polyarchy  AID000 AID_2 SAID100 SAID000 ODA100 ODA000 PRF01 DIF07 FPP01 POL05 POL25 L203 L2999a L220 L223 L221 L225 L222 L212 c.year#c.L203 c.year#c.L2999a c.year#c.L220 c.year#c.L223 c.year#c.L221 c.year#c.L225 c.year#c.L222 c.year#c.L212 || countrycode: year, vce(robust) residuals(ar1, t(year))


************************IDing largest changes****************************

xtset countrycode year

*use polyarchy, broadest v-dem measure

*difference polyarchy and aid100
gen v2x_polyarchychange=d.v2x_polyarchy
gen AID100change=d.AID100

*mean change super small---.008, but SD=.047404
sum v2x_polyarchychange
sum AID100change

*tag observations with a standard deviation change in aid and in democracy, get rid of first year when everyone will be tagged

gen SDincreasedemoDGaid = 0
replace SDincreasedemoDGaid=1 if v2x_polyarchychange>.0474 & AID100change>4.49
replace SDincreasedemoDGaid=0 if year==1990

*lags here---have to add 1991 to restricted list because of lag
gen SDincreasedemoDGaidlagaid = 0
replace SDincreasedemoDGaidlagaid=1 if v2x_polyarchychange>.0474 & l.AID100change>4.49
replace SDincreasedemoDGaidlagaid=0 if year==1990 | year==1991


*reductions
gen SDdecreasedemoDGaid = 0
replace SDdecreasedemoDGaid=1 if v2x_polyarchychange<-.0474 & AID100change<-4.49
replace SDdecreasedemoDGaid=0 if year==1990


*finish